package org.example.factory.mysql;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import org.example.entity.UserInfo;
import org.example.model.mysql.Account;
import org.example.model.mysql.ItemInfo;
import org.example.model.mysql.Statistic;
import org.example.utils.RandomUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huangy
 * @description 用户信息工厂
 * @since 2022/11/11 16:34
 **/
public class UserInfoFactory {

    private static long id = 100000000;

    public static List<UserInfo> createUserInfo(long num) {
        List<UserInfo> userInfos = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id++);
            userInfo.setAccount(JSON.toJSONString(createAccount()));
            userInfo.setProps(JSON.toJSONString(createProps(5)));
            userInfo.setItems(JSON.toJSONString(createItems(10)));
            userInfo.setStatistic(JSON.toJSONString(createStatistic()));
            userInfos.add(userInfo);
        }
        return userInfos;
    }

    /*
     * @description 创建账号
     * @author huangy
     * @since 2022/11/11 16:49
     * @return org.example.model.mysql.Account
     **/
    private static Account createAccount() {
        Account account = new Account();
        account.setNick(RandomUtils.randomChineseName());
        account.setGender(RandomUtils.randomZeroOrOne());
        account.setIp("192.168.25.124");
        account.setCountry("USA");
        account.setChannel("channel");
        account.setGameId(IdUtil.getSnowflakeNextId());
        return account;
    }

    /*
     * @description 创建属性map
     * @author huangy
     * @since 2022/11/11 16:53
     * @return java.util.Map<java.lang.Integer,java.lang.Integer>
     **/
    public static Map<Integer, Integer> createProps(int propNum) {
        Map<Integer, Integer> props = new HashMap<>();
        for (int i = 0; i < propNum; i++) {
            props.put(i + 1, i + 1);
        }
        return props;
    }

    /*
     * @description 创建道具map
     * @author huangy
     * @since 2022/11/11 16:54
     * @return java.util.Map<java.lang.Integer,java.lang.Integer>
     **/
    private static Map<Integer, ItemInfo> createItems(int itemNum) {
        Map<Integer, ItemInfo> items = new HashMap<>();
        for (int i = 0; i < itemNum; i++) {
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.setId(i + 1);
            itemInfo.setNum(10);
            itemInfo.setExpireTime(LocalDateTime.now());
            itemInfo.setIsUsing(RandomUtils.randomZeroOrOne());
            items.put(itemInfo.getId(), itemInfo);
        }
        return items;
    }

    private static Statistic createStatistic() {
        Statistic statistic = new Statistic();
        statistic.setWin(RandomUtils.RANDOM.nextInt(100));
        statistic.setLost(RandomUtils.RANDOM.nextInt(100));
        statistic.setDraw(RandomUtils.RANDOM.nextInt(100));
        statistic.setEscape(RandomUtils.RANDOM.nextInt(100));
        statistic.setMaxExp(RandomUtils.RANDOM.nextInt(100000));
        return statistic;
    }

    public static void main(String[] args) {
        List<UserInfo> userInfos = createUserInfo(10);
        userInfos.forEach(System.out::println);
    }

    private UserInfoFactory() {
        throw new UnsupportedOperationException();
    }

}
